/*
 * Copyright (C) 2013, Pavel Samko <bulldozerbsg@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the "Pavel Samko" nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

#include "vfscore.h"
#include "vfslib.h"
#include "vfsrealfs.h"
#include "vfsvirtfs.h"
#include "vfsutils.h"
#include <stdlib.h>
#include <string.h>

VFS_PARAMS params_save[] =
{
	{VFS_PARAM_PATH_HAS, &g_save_path_has, sizeof(g_save_path_has)},
	{VFS_PARAM_PATH, g_save_path, sizeof(g_save_path)},
};

#define PARAMS_SAVE_COUNT (sizeof(params_save) / sizeof(params_save[0]))

VFS_PARAMS params_data[] =
{
	{VFS_PARAM_PATH_HAS, &g_data_path_has, sizeof(g_data_path_has)},
	{VFS_PARAM_PATH, g_data_path, sizeof(g_data_path)},
};

#define PARAMS_DATA_COUNT (sizeof(params_data) / sizeof(params_data[0]))

MOD g_real_mods[REAL_MOD_COUNT] =
{
	{
		"Real FS Save",
		vfs_realfs_fclose,
		vfs_realfs_fflush,
		vfs_realfs_fopen,
		vfs_realfs_fread,
		vfs_realfs_fseek,
		vfs_realfs_ftell,
		vfs_realfs_fwrite,
		NULL,
		params_save,
		PARAMS_SAVE_COUNT
	},
	{
		"Real FS Data",
		vfs_realfs_fclose,
		NULL,
		vfs_realfs_fopen,
		vfs_realfs_fread,
		vfs_realfs_fseek,
		vfs_realfs_ftell,
		NULL,
		NULL,
		params_data,
		PARAMS_DATA_COUNT
	}
};

MOD g_virt_mods[VIRT_MOD_COUNT];
size_t g_virt_mods_count;

HANDLER g_virt_handlers[VIRT_HANDLER_COUNT] =
{
	{
		"Virt FS Handler",
		vfs_virtfs_probe_vfs,
		NULL
	}
};
size_t g_virt_handlers_count = 1;

int g_data_path_has;
int g_save_path_has;
char g_data_path[REALFS_NAME_SIZE];
char g_save_path[REALFS_NAME_SIZE];

int vfs_core_add_mod(const MOD * mod)
{
	if (mod && (g_virt_mods_count < VIRT_MOD_COUNT))
	{
		memcpy(&g_virt_mods[g_virt_mods_count], mod, sizeof(MOD));
		++g_virt_mods_count;
		return 1;
	}
	return 0;
}

void vfs_init(const char * data_path, const char * save_path)
{
	size_t l;
	if (data_path && ((l = strlen(data_path)) < (REALFS_NAME_SIZE - 2)))
	{
		strcpy(g_data_path, data_path);
#ifdef _WIN32
		vfs_strbstos(g_data_path);
#endif
		if (l && (g_data_path[l - 1] != '/'))
		{
			g_data_path[l] = '/';
			g_data_path[l + 1] = 0;
		}
		g_data_path_has = 1;
	}
	if (save_path && ((l = strlen(save_path)) < (REALFS_NAME_SIZE - 2)))
	{
		strcpy(g_save_path, save_path);
#ifdef _WIN32
		vfs_strbstos(g_save_path);
#endif
		if (l && (g_save_path[l - 1] != '/'))
		{
			g_save_path[l] = '/';
			g_save_path[l + 1] = 0;
		}
		g_save_path_has = 1;
	}
}

int vfs_addfs(const char * name, VFS_PARAMS * params, size_t params_count)
{
	size_t i;
	int result = 0;
	VFILE * f = vfs_fopen(name, "rb");
	if (f)
	{
		for (i = 0; i < g_virt_handlers_count; ++i)
		{
			if (g_virt_handlers[i].f_probe_vfs && (result = g_virt_handlers[i].f_probe_vfs(f, params, params_count)))
			{
				break;
			}
		}
		vfs_fclose(f);
	}
	return result;
}
